package com.sykj.weichat.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sykj.common.WeChatConfig;
import com.sykj.util.SignUtil;


@Controller
@RequestMapping("/weichat")
public class WeichatController {
	static final String token = "sykj2018";
	/**
	 * 接口微信服务
	 * @param signature 微信签名
	 * @param timestamp 时间
	 * @param nonce 随机数
	 * @param echostr 随机字符串
	 * @param request
	 * @return
	 */
	@RequestMapping("interface")
	public void duijie(String signature,String timestamp,String nonce,String echostr,HttpServletResponse Response,HttpSession session){
		System.out.println("signature"+signature+"timestamp"+timestamp+"nonce"+nonce+"echostr"+echostr);
		WeChatConfig.WECHAT_CONFIG.put("signature", signature);
		WeChatConfig.WECHAT_CONFIG.put("timestamp", timestamp);
		WeChatConfig.WECHAT_CONFIG.put("nonce", nonce);
		WeChatConfig.WECHAT_CONFIG.put("echostr", echostr);
		boolean flag = SignUtil.checkSignature(token, signature, timestamp, nonce);
		//做了签名检查
		if (flag) {
			try {
				Response.setCharacterEncoding("utf-8");
				Response.setHeader("Content-type", "text/html;charset=UTF-8");
				PrintWriter pw = Response.getWriter();
				pw.write(echostr);//返给微信请求字符串
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
